package common;

import java.awt.Color;

import javax.swing.ImageIcon;
import javax.swing.text.AttributeSet;
import javax.swing.text.SimpleAttributeSet;
import javax.swing.text.StyleConstants;

public class StyleBean implements java.io.Serializable{
	/**
	 * 
	 */
	private static final long serialVersionUID = 1L;
    public final static String LINK_ATTRIBUTE = "isLink";
	private String fontName;
	private int	fontSize;
	private boolean isBold;
	private boolean isItalic;
	private boolean isUnderine;
	private boolean isLink;
	private Color color;
	private String iconName;
	
	public StyleBean(){
	}
	
	public StyleBean(AttributeSet attr){
		this();
		this.loadAttributeSet(attr);
	}

	public String getIconName() {
		return iconName;
	}

	public void setIconName(String iconName) {
		this.iconName = iconName;
	}

	public String getFontName() {
		return fontName;
	}
	public void setFontName(String fontName) {
		this.fontName = fontName;
	}
	public int getFontSize() {
		return fontSize;
	}
	public void setFontSize(int fontSize) {
		this.fontSize = fontSize;
	}
	public boolean isBold() {
		return isBold;
	}
	public void setBold(boolean isBold) {
		this.isBold = isBold;
	}
	public boolean isItalic() {
		return isItalic;
	}
	public void setItalic(boolean isItalic) {
		this.isItalic = isItalic;
	}
	public boolean isUnderine() {
		return isUnderine;
	}

	public boolean isLink() {
		return isLink;
	}

	public void setLink(boolean isLink) {
		this.isLink = isLink;
	}

	public void setUnderine(boolean isUnderine) {
		this.isUnderine = isUnderine;
	}

	public Color getColor() {
		return color;
	}
	public void setColor(Color color) {
		this.color = color;
	}

	public void loadAttributeSet(AttributeSet attr){
        this.setFontName(StyleConstants.getFontFamily(attr));
        this.setFontSize(StyleConstants.getFontSize(attr));
        this.setBold(StyleConstants.isBold(attr));
        this.setItalic(StyleConstants.isItalic(attr));
        this.setUnderine(StyleConstants.isUnderline(attr));
        this.setColor(StyleConstants.getForeground(attr));
        if(attr.getAttribute(LINK_ATTRIBUTE)!=null)
        	this.setLink((Boolean) attr.getAttribute(LINK_ATTRIBUTE));
        ImageIcon ico = (ImageIcon) StyleConstants.getIcon(attr);
        if(ico!=null)
        	this.setIconName(ico.getDescription()+".png");
	}
	
	public AttributeSet getAttributeSet(){
		SimpleAttributeSet attr = new SimpleAttributeSet();
		StyleConstants.setFontFamily(attr, this.getFontName());
		StyleConstants.setFontSize(attr, this.getFontSize());
		StyleConstants.setBold(attr, this.isBold());
		StyleConstants.setItalic(attr, this.isItalic());
		StyleConstants.setForeground(attr, this.getColor());
		StyleConstants.setUnderline(attr, this.isUnderine());
		attr.addAttribute(LINK_ATTRIBUTE, this.isLink());
		if(this.getIconName()!=null){
			StyleConstants.setIcon(attr,new ImageIcon(Utilities.createImage("resource/emotion/"+this.getIconName(), this.getIconName())));
		}
		return attr;
	}
}
